<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title></title>
</head>
<body>
<!--弹窗：单据借款-->
<div th:fragment="pzcurmainLoan" class="modal inmodal fade" id="pzcurmainLoan_Modal"
     tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title" id="pzcurmainLoan_modalTitle">单据借款</h4>
                <p id="pzcurmainLoan_modalDesc" class="font-bold text-danger"></p>
            </div>
            <div class="modal-body">
                <form id="pzcurmainLoan_Form">
                    <input id="pzcurmainLoan_id" name="id" type="hidden"/>
                    <input id="pzcurmainLoan_applyCode" name="applyCode" type="hidden"/>
                    <input id="pzcurmainLoan_loanType" name="loanType" value="1" type="hidden"/>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group"><label>借款金额(元)</label>
                                <input id="sysmenuAddOrUpdate_loanAmount" name="loanAmount"
                                       type="text" placeholder="借款金额" class="form-control"/>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group"><label>支付方式</label>
                                <select class="form-control m-b" id="sysmenuAddOrUpdate_moneyType" name="moneyType">
                                    <option value="1">现金</option>
                                    <option value="2">转账</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="pzcurmainLoanObj.submitHandler()">
                    保存
                </button>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        $(function () {
            $('#pzcurmainLoan_Form').validate({
                rules: pzcurmainLoanObj.dataRule,
                submitHandler: function (form) {
                    pzcurmainLoanObj.dataFormSubmit(form);
                }
            });
        })
        var pzcurmainLoanObj = new Object({
            formData: {
                applyCode: "",
                money: 0
            },
            dataRule: {
                loanAmount: {
                    required: true,
                    number: true,
                    min: 0.01,
                    toDecimal2: true
                }
            },
            init: function (applyCode, money) {
                pzcurmainLoanObj.formData.applyCode = applyCode;
                pzcurmainLoanObj.formData.money = money;
                $.myGet({
                    url: `/pz/cur/pzloan/info/${applyCode}`,
                    success: function (data) {
                        if (data && data.code === 0) {
                            pzcurmainLoanObj.fillForm(data.loanDetail ? data.loanDetail : {});
                        } else {
                            toastr.error(data.msg, '操作失败')
                        }
                    }
                })
            },
            fillForm: function (data = {}) {
                data.applyCode = pzcurmainLoanObj.formData.applyCode;
                if (!data.loanAmount) {
                    data.loanAmount = pzcurmainLoanObj.formData.money;
                }
                let formArray = $("#pzcurmainLoan_Form").serializeArray();
                for (let i in formArray) {
                    if (formArray[i].name != 'loanType') {
                        $('input[name=' + formArray[i].name + ']').val(data[formArray[i].name])
                    }
                }
            },
            submitHandler: function () {
                $("#pzcurmainLoan_Form").submit();
            },
            dataFormSubmit: function (form) {
                $.mySwalConfirm(`将保存借款信息，确认吗？`, function () {
                    let formVals = $(form).serializeArray();
                    let data = {}
                    for (let i = 0; i < formVals.length; i++) {
                        data[formVals[i].name] = formVals[i].value;
                    }
                    $.myPost({
                        url: `/pz/cur/pzloan/save`,
                        dataType: 'json',
                        data: data,
                        success: function (data) {
                            if (data && data.code == 0) {
                                toastr.success(data.msg, '操作提示')
                                $('#pzcurmainLoan_Modal').modal('hide')
                                pzcurmainListObj.refreshDataList();
                            } else {
                                toastr.error(data.msg, '操作失败')
                            }
                        }
                    })
                });
            }
        })
    </script>
</div>
</body>
</html>